package com.canbot.u05.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;

import java.io.IOException;

/**
 * Created by zmp on 2017/8/3.
 */

public class SoundManager {

        private static SoundManager mSoundManager;

        private boolean isPause;

        public static SoundManager getInstance() {
                if (mSoundManager == null) {
                        mSoundManager = new SoundManager();
                }
                return mSoundManager;
        }

        private SoundManager() {
                this.mMediaPlayer = new MediaPlayer();
        }



        /**
         * 获取音频播放状�??
         *
         * @param
         * @return boolean
         */
        public final boolean isPlaying() {
                if (mMediaPlayer != null) {
                        return mMediaPlayer.isPlaying();
                }
                return false;
        }

        private MediaPlayer mMediaPlayer;

        private String TAG = "SoundManager";

        private Context mContext;

        public void init(Context context) {
                this.mContext = context;
        }


        /**
         * 播放sd下制定路径或 url 的一个音频文件.
         *
         * @param path
         */
        public final void playSound(final String path, final IPlayStateListener listener) {
                resetSound();
                Logger.d(TAG, "playSound file time len=" + path);
                try {
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        try {
                                mMediaPlayer.setDataSource(path);
                        }
                        catch (IllegalArgumentException | SecurityException
                                | IllegalStateException | IOException e) {
                                e.printStackTrace();

                                if (listener != null) {
                                        listener.playResultState(false, null);
                                }
//                return;
                                throw new RuntimeException("paly +" + path + "\r\n" + e.getLocalizedMessage());
                        }
                        mMediaPlayer.prepareAsync();
                        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                                @Override
                                public void onPrepared(MediaPlayer arg0) {
                                        Logger.d(TAG, "playSound file time len=" + mMediaPlayer.getDuration());
                                        mMediaPlayer.start();
                                }
                        });

                        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                public void onCompletion(MediaPlayer mp) {
                                        if (listener != null) {
                                                listener.playResultState(true, null);
                                        }

                                }
                        });
                }
                catch (Exception e) {
                        e.printStackTrace();
                        Logger.e(TAG, "Play Exception--:" + e.toString());
                        if (listener != null) {
                                listener.playResultState(false, null);
                        }
                        throw new RuntimeException(e);
                }
        }

        /**
         * 播放sd下制定路径或 url 的一个音频文件.
         *
         * @param path
         */
        public final void playSound(final String path, final IPlayStateListener listener, final int seek) {
                resetSound();
                Logger.d(TAG, "playSound file time len=" + path);
                try {
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        try {
                                mMediaPlayer.setDataSource(path);
                        }
                        catch (IllegalArgumentException | SecurityException
                                | IllegalStateException | IOException e) {
                                e.printStackTrace();

                                if (listener != null) {
                                        listener.playResultState(false, null);
                                }
//                return;
                                throw new RuntimeException("paly +" + path + "\r\n" + e.getLocalizedMessage());
                        }
                        mMediaPlayer.prepareAsync();
                        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                                @Override
                                public void onPrepared(MediaPlayer arg0) {
                                        Logger.d(TAG, "playSound file time len=" + mMediaPlayer.getDuration());
                                        mMediaPlayer.seekTo(seek);
                                        mMediaPlayer.start();
                                }
                        });

                        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                public void onCompletion(MediaPlayer mp) {
                                        resetSound();
                                        if (listener != null) {
                                                listener.playResultState(true, null);
                                        }
                                }
                        });
                }
                catch (Exception e) {
                        e.printStackTrace();
                        Logger.e(TAG, "Play Exception--:" + e.toString());
                        if (listener != null) {
                                listener.playResultState(false, null);
                        }
                        throw new RuntimeException(e);
                }
        }

        /**
         * stopSound
         */
        public final void destroy() {
                Logger.e(TAG, "停止音频播放");
                if (mMediaPlayer != null) {
                        if (mMediaPlayer.isPlaying()) {
                                mMediaPlayer.stop();
                                mMediaPlayer.reset();
                        }
                        mMediaPlayer.release();
                        mMediaPlayer = null;
                }
                mSoundManager = null;
        }


        /**
         * stopSound
         */
        public final int pauseSound() {
                Logger.e(TAG, "停止音频播放");
                if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                        isPause = true;
                        mMediaPlayer.pause();
                        return mMediaPlayer.getCurrentPosition();
                }
                return 0;
        }

        /**
         * restartSound
         */
        public final void restartPlay() {
                if (isPause) {
                        mMediaPlayer.start();
                }
        }

        /**
         * resetSound
         */
        public final void resetSound() {
                Logger.d(TAG, "resetSound");
                isPause = false;
                if (mMediaPlayer != null) {
                        if (mMediaPlayer.isPlaying()) {
                                mMediaPlayer.pause();
                        }
                        mMediaPlayer.setOnPreparedListener(null);
                        mMediaPlayer.reset();
                }
        }

        interface IPlayStateListener{
                void playResultState(boolean isSuccess, String content);
        }
}
